package sdk.webview.fmc.com.fmcsdk.util.retrofit2.observer;


import android.content.Context;
import android.os.Build;
import android.preference.PreferenceManager;

import androidx.annotation.RequiresApi;

import java.net.Proxy;
import java.util.concurrent.TimeUnit;

import okhttp3.ConnectionPool;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.fastjson.FastJsonConverterFactory;
import sdk.webview.fmc.com.fmcsdk.util.Constant;
//import sdk.webview.fmc.com.fmcsdk.util.HttpsManager;
import sdk.webview.fmc.com.fmcsdk.util.HttpsManager;
import sdk.webview.fmc.com.fmcsdk.util.HttpsUtils;
import sdk.webview.fmc.com.fmcsdk.util.MySSLSocketClient;
import sdk.webview.fmc.com.fmcsdk.util.OkHostnameVerifier;
import sdk.webview.fmc.com.fmcsdk.util.SSLSocketUtils;
import sdk.webview.fmc.com.fmcsdk.util.Utility;
import sdk.webview.fmc.com.fmcsdk.util.XLog;
import sdk.webview.fmc.com.fmcsdk.util.retrofit2.CustomTrust;
//import retrofit2.converter.jackson.JacksonConverterFactory;

/**
 * 用于获取配置好的retrofit对象, 通过设置 Configuration#enableLoggingNetworkParams(来启用网络请求
 * 参数与相应结果.
 * <br/>
 * Created by Ryan on 2015/12/30.
 */
public class RetrofitFactory {
    private static OkHttpClient client;
    private static Retrofit retrofit;
    private static String baseUrl;
    private static Context contexts;

    public static void init(String url, Context context) {
        baseUrl = url;
        contexts = context;
    }

    /**
     * 获取配置好的retrofit对象来生产Manager对象
     */

    @RequiresApi(api = Build.VERSION_CODES.N)
    public static Retrofit getRetrofit() {
        if (retrofit == null) {
            if (baseUrl == null || baseUrl.length() <= 0)
                throw new IllegalStateException("请在调用getFactory之前先调用setBaseUrl");
            Retrofit.Builder builder = new Retrofit.Builder();
            builder.baseUrl(baseUrl).addCallAdapterFactory(RxJavaCallAdapterFactory.create()).addConverterFactory(FastJsonConverterFactory.create());

            boolean isSdk = PreferenceManager.getDefaultSharedPreferences(contexts).getBoolean(Constant.IS_SDK, false);
            //如果不是SDK的话验证自建证书，否则跳过验证
            if (!isSdk) {
                String appName = (String) Utility.getMetaData(contexts, Constant.APP_NAME);
                XLog.i("appName:" + appName);
                String isHttps = (String) Utility.getMetaData(contexts, Constant.IS_HTTPS_TEST);
                XLog.i("isHttps:" + isHttps);
                if (isHttps.equals("fmc_true")) {
//                    //宝安区和港机的要检验测试环境的自签证书
//                    client = new OkHttpClient.Builder().connectTimeout(20, TimeUnit.SECONDS).sslSocketFactory(HttpsManager.getSslSocketFactory(contexts)).build();
                    client = new OkHttpClient.Builder()
                            .connectTimeout(50, TimeUnit.SECONDS)
                            .readTimeout(50, TimeUnit.SECONDS)
                            .writeTimeout(50, TimeUnit.SECONDS)
                            .connectionPool(new ConnectionPool(5, 5, TimeUnit.MINUTES))
                            .sslSocketFactory(HttpsUtils.setSafeOkHttpClient())
                            .build();
//                    client = new OkHttpClient.Builder().connectTimeout(20, TimeUnit.SECONDS).sslSocketFactory(SSLSocketUtils.newSslSocketFactory()).hostnameVerifier(OkHostnameVerifier.INSTANCE).build();
                } else {
                    MyHttpLoggingInterceptor interceptor = new MyHttpLoggingInterceptor();

                    //正式证书环境
                    client = new OkHttpClient.Builder()
                            .connectTimeout(50, TimeUnit.SECONDS)
                            .readTimeout(50, TimeUnit.SECONDS)
                            .writeTimeout(50, TimeUnit.SECONDS)
                            .connectionPool(new ConnectionPool(5, 5, TimeUnit.MINUTES))
                            .addInterceptor(interceptor)
                            //注意此处
                        //    .sslSocketFactory(MySSLSocketClient.getSSLSocketFactory(), MySSLSocketClient.X509)
                            .hostnameVerifier(MySSLSocketClient.getHostnameVerifier())

                            .build();
                }
            } else {
                client = new OkHttpClient.Builder()
                        .connectTimeout(50, TimeUnit.SECONDS)
                        .readTimeout(50, TimeUnit.SECONDS)
                        .writeTimeout(50, TimeUnit.SECONDS)
                        .connectionPool(new ConnectionPool(5, 5, TimeUnit.MINUTES))
                        .build();
            }

            builder.client(client);

            retrofit = builder.build();
        }

        return retrofit;
    }

    public static void clearAllRequest() {
        if (client != null) {
            client.dispatcher().cancelAll();
        }
    }
}
